using System;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections.Generic;
using WebSite.Admin.Core;

namespace WebSite.Admin
{
	public partial class Admin_PrizeUserList : AdminBasePage
	{
	
	// Coded by wenbin 2012-10-25 11:12:53
		#region 实体类
		/// <summary>
		/// 实体类
		/// </summary>
		private class PrizeUserList
		{
			private int _id = 0;	
			public  int  Id  
			{
				get{ return  _id; }
				set{ _id = value; }
			}
			private string _title = string.Empty;	
			public  string  Title  
			{
				get{ return  _title; }
				set{ _title = value; }
			}
			private string _userList = string.Empty;	
			public  string  UserList  
			{
				get{ return  _userList; }
				set{ _userList = value; }
			}
			private int _orderBy = 0;	
			public  int  OrderBy  
			{
				get{ return  _orderBy; }
				set{ _orderBy = value; }
			}
			private int _type = 0;	
			public  int  Type  
			{
				get{ return  _type; }
				set{ _type = value; }
			}
	
	}
		#endregion
	
		dbHelper db = new dbHelper();
		
		/// <summary>
        /// 查询id
        /// </summary>
        public long QId
        {
            get
            {
                return Convert.ToInt64(Request.QueryString["id"]);
            }
        }
		
		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);
			
		}
	
	
		protected void Page_Load(object sender, EventArgs e)
		{
			string redirectUrl = HttpContext.Current.Request.Url.ToString().Split('?')[0];
			int page = Convert.ToInt32(Request.QueryString["page"]);
			if (page == 0)
				page = 1;
			string urlPage = "page=" + page;
			string strWhere = " 1=1 ";
			string urlParams = "";
			string searchId= dbHelper.FilterSql(Request.QueryString["searchId"]);
			if (!string.IsNullOrEmpty(searchId))
			{
				strWhere +=" and Id ="+searchId+"";
				urlParams += "&searchId=" + searchId;
			}
			string searchTitle= dbHelper.FilterSql(Request.QueryString["searchTitle"]);
			if (!string.IsNullOrEmpty(searchTitle))
			{
				strWhere +=" and Title like '%"+searchTitle+"%'";
				urlParams += "&searchTitle=" + searchTitle;
			}
			string searchUserList= dbHelper.FilterSql(Request.QueryString["searchUserList"]);
			if (!string.IsNullOrEmpty(searchUserList))
			{
				strWhere +=" and UserList like '%"+searchUserList+"%'";
				urlParams += "&searchUserList=" + searchUserList;
			}
			string searchOrderBy= dbHelper.FilterSql(Request.QueryString["searchOrderBy"]);
			if (!string.IsNullOrEmpty(searchOrderBy))
			{
				strWhere +=" and OrderBy ="+searchOrderBy+"";
				urlParams += "&searchOrderBy=" + searchOrderBy;
			}
			string searchType= dbHelper.FilterSql(Request.QueryString["searchType"]);
			if (!string.IsNullOrEmpty(searchType))
			{
				strWhere +=" and Type ="+searchType+"";
				urlParams += "&searchType=" + searchType;
			}
			
			urlPage = urlPage + urlParams;
			if(!string.IsNullOrEmpty(urlParams))
				urlParams = urlParams.Substring(1, urlParams.Length - 1);
			redirectUrl = redirectUrl + "?" + urlPage;
			
			//数据导出
	 		string toExcel = dbHelper.FilterSql(Request.QueryString["toExcel"]);
            if (toExcel == "1")
            {
                ExcelSubmit(strWhere);
            }
	
			//分页
			Paging(page, strWhere, urlParams);
	
			//业务
			BussessOper(redirectUrl);
	
		
		}
	
	  	#region  --数据导出---
       
        /// <summary>
        /// 保存xls文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ExcelSubmit(string sWhere)
        {		 	
			string sSelect = "Id as 'Id',Title as '标题',UserList as '用户列表',OrderBy as '排序',Type as 'Type'";
			
            string some_search = string.Format("select {0} from PrizeUserList(nolock) where {1} ", sSelect, sWhere); //查询语句
            DataSet ds = db.returndataset(null, some_search, null, null);

            //填冲到XLS中////
            AdminCommon.xlsGridview(ds, string.Format("数据_{0}", DateTime.Now));
        }

        #endregion
	
	
		#region 分页
		public void Paging(int page, string strWhere, string urlParams)
		{
			dbHelper.pageproret record = new dbHelper.pageproret();
	
			record = db.GetPageRecordSet("PrizeUserList", page, 20, "Id", "Id Desc", "*", strWhere);
			int pagecount = record.pagecount;
			DataTable dt = record.recordset;
			List<PrizeUserList> datasrc = db.ConvertTo<PrizeUserList>(dt).ToList();
				
			 DataList.DataSource = datasrc.Count > 0 ? datasrc : null;
            DataList.DataBind();
		
			string pager = db.BuildPager(page, pagecount, "", urlParams);
			sPage.Text = pager;
		}
	
		#endregion
	
		#region 业务处理
		/// <summary>
		/// 业务处理
		/// </summary>
		/// <param name="redirectUrl"></param>
		public void BussessOper(string redirectUrl)
		{
			if (Request.QueryString["act"] == "del")
			{				
				if (QId != 0)
				{
					DeleteData(QId);
					Response.Redirect(redirectUrl);
				}
			}
	
			if (Request.ServerVariables["REQUEST_METHOD"] == "POST")
			{
				string idStr = dbHelper.FilterSql(Request.Form["SelectID"]);
				if (!string.IsNullOrEmpty(idStr))
				{
					idStr = idStr.Replace(" ", "");
					idStr = idStr.Replace("&nbsp;", "");
					if (!string.IsNullOrEmpty(Request.Form["delete"]))
					{
						DeleteBatchData(idStr);
						Response.Redirect(redirectUrl);
					}
					if (!string.IsNullOrEmpty(Request.Form["pingbi"]))
					{
						SwitchBatchData(0, idStr);
						Response.Redirect(redirectUrl);
					}
					if (!string.IsNullOrEmpty(Request.Form["fabu"]))
					{
						SwitchBatchData(1, idStr);
						Response.Redirect(redirectUrl);
					}
					Response.Redirect(redirectUrl);
					Response.End();
				}
				PrizeUserList sdata = new PrizeUserList();
				
				//string modid = Request.Form["ModId"] == null ? "0" : Request.Form["ModId"].Trim();
				//int id =sdata.Id = Convert.ToInt32(modid);
					sdata.Id  = Convert.ToInt32(dbHelper.FilterSql(Request.Form["ModId"].Trim())?? "");
					sdata.Title  = Convert.ToString(dbHelper.FilterSql(Request.Form["ModTitle"].Trim())?? "");
					sdata.UserList  = Convert.ToString(dbHelper.FilterSql(Request.Form["ModUserList"].Trim())?? "");
					sdata.OrderBy  = Convert.ToInt32(dbHelper.FilterSql(Request.Form["ModOrderBy"].Trim())?? "");
					sdata.Type  = Convert.ToInt32(dbHelper.FilterSql(Request.Form["ModType"].Trim())?? "");
				var id = sdata.Id;
				if (id == 0)
				{
					//增加
					if (InsertData(sdata))
					{
						Response.Redirect(redirectUrl);
						Response.End();
					}
					else
					{
						Response.Write("error");
						Response.End();
					}
				}
				else
				{
					//修改
					if (UpdateData(sdata))
					{
						Response.Redirect(redirectUrl);
						Response.End();
					}
					else
					{
						Response.Write("error");
						Response.End();
					}
				}
			}
		}
		#endregion
		
		
		#region 增删改
		
		/// <summary>
		/// 添加数据
		/// </summary>
		/// <param name="sdata"></param>
		/// <returns></returns>
		private bool InsertData(PrizeUserList sdata)
		{
			string sqlStr = @"INSERT INTO PrizeUserList(
					[Title],
							[UserList],
							[OrderBy],
							[Type]	)VALUES(
					@Title,
							@UserList,
							@OrderBy,
							@Type)";
			SqlParameter[] parms = new SqlParameter[] {
					new SqlParameter("@Title",sdata.Title),
							new SqlParameter("@UserList",sdata.UserList),
							new SqlParameter("@OrderBy",sdata.OrderBy),
							new SqlParameter("@Type",sdata.Type)};
			int ret = db.GetExecuteNonQuery(null, sqlStr, parms);
			return ret > 0;
		}
		
		
		/// <summary>
		/// 修改数据
		/// </summary>		
		/// <param name="sdata"></param>
		/// <returns></returns>
		private bool UpdateData(PrizeUserList sdata)
		{
			string sqlStr = @"UPDATE PrizeUserList SET
					[Title]=@Title,
										[UserList]=@UserList,
										[OrderBy]=@OrderBy,
										[Type]=@Type WHERE Id=@Id";
				
				SqlParameter[] parms = new SqlParameter[] {
					new SqlParameter("@Id",sdata.Id),
										new SqlParameter("@Title",sdata.Title),
										new SqlParameter("@UserList",sdata.UserList),
										new SqlParameter("@OrderBy",sdata.OrderBy),
										new SqlParameter("@Type",sdata.Type)};
			int ret = db.GetExecuteNonQuery(null, sqlStr, parms);
			return ret > 0;
		}
		
		
		/// <summary>
		///单个删除
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		private bool DeleteData(long id)
		{
			string sqlStr = string.Format("DELETE FROM PrizeUserList WHERE Id={0}",id);
			
			int ret = db.GetExecuteNonQuery(null, sqlStr, null);
			return ret > 0;
		}
		/// <summary>
		/// 批量删除
		/// </summary>
		/// <param name="idStr"></param>
		/// <returns></returns>
		private bool DeleteBatchData(string idStr)
		{
			string sqlStr = string.Format("DELETE FROM PrizeUserList WHERE Id IN ({0})",idStr);
	
			int ret = db.GetExecuteNonQuery(null, sqlStr, null);
			return ret > 0;
		}
		
		
		/// <summary>
		/// 状态操作
		/// </summary>
		/// <param name="optype"></param>
		/// <param name="idStr"></param>
		/// <returns></returns>
		private bool SwitchBatchData(int optype,string idStr)
		{
			string sqlStr = "";
			if(optype==0)
				sqlStr = string.Format("UPDATE PrizeUserList SET [Status]=0 WHERE Id IN ({0})",idStr);
			else
				sqlStr = string.Format("UPDATE PrizeUserList SET [Status]=1 WHERE Id IN ({0})", idStr);
			
			int ret = db.GetExecuteNonQuery(null, sqlStr, null);
			return ret > 0;
		}
		

		
		#endregion
	}
}


